package com.itcast.cn;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet(urlPatterns = "/demo3")
public class ServletDemo3 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("当前的虚拟目录是:   "+req.getContextPath());
        System.out.println("当前jsp中使用的路径是："+req.getRequestURI());
        System.out.println("网页中输入的全路径是："+req.getRequestURL());
        System.out.println("获取请求参数："+req.getQueryString());


        String name = req.getParameter("name");
        resp.setHeader("content-type","text/html;charset=utf-8");
        resp.getWriter().write("<h1>"+name+"欢迎您！</h1>");

        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()){
            System.out.println("依次查询："+headerNames.nextElement());
        }

        String header = req.getHeader("user-agent");
        System.out.println("handr===   "+header);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        System.out.println("post....");
       //获取请求体
       //文字一般是字符流
       //图片一般是字节流
        System.out.println("获取请求参数："+req.getQueryString());
        //未知用字符流
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        System.out.println(s);
    }
}
